package com.cn.springcloudwechat.serviceImpl.vaccine;

import java.text.SimpleDateFormat;
import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.cn.springcloudwechat.common.mybatis.Page;
import com.cn.springcloudwechat.common.utils.Const;
import com.cn.springcloudwechat.common.utils.EmptyUtil;
import com.cn.springcloudwechat.common.utils.ErrorUtil;
import com.cn.springcloudwechat.common.utils.ThreadLocalUtil;
import com.cn.springcloudwechat.common.utils.Util;
import com.cn.springcloudwechat.mapper.mapper.vaccine.YmChildrenOrderVaccinationMapper;
import com.cn.springcloudwechat.mapper.mapper.vaccine.YmVaccineMapper;
import com.cn.springcloudwechat.mapper.mapper.vaccine.YmVaccineRelevantMapper;
import com.cn.springcloudwechat.mapper.mapper.vaccine.YmVaccineTypeMapper;
import com.cn.springcloudwechat.model.YmChildrenOrderVaccination;
import com.cn.springcloudwechat.model.YmUser;
import com.cn.springcloudwechat.model.YmUserChild;
import com.cn.springcloudwechat.model.vaccine.YmVaccine;
import com.cn.springcloudwechat.model.vaccine.YmVaccineRelevant;
import com.cn.springcloudwechat.model.vaccine.YmVaccineTypeWithBLOBs;
import com.cn.springcloudwechat.service.vaccine.vaccineService.VaccineService;

@Service("VaccineService")
public class VaccineServiceImpl implements VaccineService {
	@Autowired
	YmVaccineMapper ymVaccineMapper;
	@Autowired
	YmVaccineTypeMapper ymVaccineTypeMapper;
	@Autowired
	YmVaccineRelevantMapper ymVaccineRelevantMapper;
	@Autowired
	YmChildrenOrderVaccinationMapper ymChildrenOrderVaccinationMapper;
	
	/**
	 * 疫苗的发布(疫苗的类型(详情),疫苗的关联参数)
	 * 
	 * @param YmVaccine ymVaccine,String[] parameter
	 *           
	 * @return SUCC
	 */
	@Override
	public void insertVaccineSer(YmVaccine ymVaccine/*,String[] parameter*/) {
		try {
			Assert.state(!EmptyUtil.objFieldsIsEmpty(ymVaccine),String.format(ErrorUtil.PARAMETER_NULL,"ymVaccine"));
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("vaccineTypeId",ymVaccine.getVaccineTypeId());
			map.put("age",ymVaccine.getAge());
			Integer ex = ymVaccineMapper.queryIfExist(map);
			if(ex > 0){
			throw new IllegalArgumentException(String.format(ErrorUtil.VACCINE_ERROR_IF));
			}
			ymVaccineMapper.insertSelective(ymVaccine);
		} catch (IllegalAccessException e) {
			throw new IllegalArgumentException(String.format(ErrorUtil.VACCINE_ERROR));
		}
	}

	/**
	 * 疫苗类型的发布(疫苗的类型(详情))
	 * 
	 * @param YmVaccineTypeWithBLOBs ymVaccineTypeWithBLOBs
	 *           
	 * @return SUCC
	 */
	@Override
	public void insertVaccineTypeSer(YmVaccineTypeWithBLOBs ymVaccineTypeWithBLOBs,String[] parameter) {
		try {
			Assert.state(!EmptyUtil.objFieldsIsEmpty(ymVaccineTypeWithBLOBs),String.format(ErrorUtil.PARAMETER_NULL,"ymVaccineTypeWithBLOBs"));
			ymVaccineTypeWithBLOBs.setState((short)1);
			ymVaccineTypeMapper.insertSelective(ymVaccineTypeWithBLOBs);
			YmVaccineRelevant YmVaccineRelevant = new YmVaccineRelevant();
			YmVaccineRelevant.setVaccineTypeId(ymVaccineTypeWithBLOBs.getVaccineTypeId());
			YmVaccineRelevant.setState((short)1);
			if(parameter!=null){
				for(String pt : parameter){
					YmVaccineRelevant.setRelevantVaccineTypeId(Long.parseLong(pt));
					ymVaccineRelevantMapper.insertSelective(YmVaccineRelevant);
				}
			}
		} catch (IllegalAccessException e) {
			throw new IllegalArgumentException(String.format(ErrorUtil.VACCINE_ERROR));
		}
	}
	
	/**
	 * 修改疫苗及关联疫苗(包括详情)
	 * 
	 * @param YmVaccineTypeWithBLOBs ymVaccineTypeWithBLOBs,String[] parameter
	 *           
	 * @return SUCC
	 */
	@Override
	public void updateVaccineTypeSer(YmVaccineTypeWithBLOBs ymVaccineTypeWithBLOBs, String[] parameter) {
		try {
			Assert.state(!EmptyUtil.objFieldsIsEmpty(ymVaccineTypeWithBLOBs),String.format(ErrorUtil.PARAMETER_NULL,"ymVaccineTypeWithBLOBs"));
			ymVaccineTypeMapper.updateByPrimaryKeySelective(ymVaccineTypeWithBLOBs);
			//删除
			ymVaccineRelevantMapper.deleteByPrimaryKeyOther(ymVaccineTypeWithBLOBs.getVaccineTypeId());
			YmVaccineRelevant YmVaccineRelevant = new YmVaccineRelevant();
			YmVaccineRelevant.setVaccineTypeId(ymVaccineTypeWithBLOBs.getVaccineTypeId());
			YmVaccineRelevant.setState((short)1);
			for(String pt : parameter){
			YmVaccineRelevant.setRelevantVaccineTypeId(Long.parseLong(pt));
			ymVaccineRelevantMapper.insertSelective(YmVaccineRelevant);			
			}
		} catch (IllegalAccessException e) {
			throw new IllegalArgumentException(String.format(ErrorUtil.VACCINE_ERROR));
		}
	}

	/**
	 * 疫苗类型的查询
	 * 
	 * @param YmVaccineTypeWithBLOBs ymVaccineTypeWithBLOBs
	 *           
	 * @return List<YmVaccineTypeWithBLOBs>
	 */
	/*@Override
	public List<YmVaccineTypeWithBLOBs> queryVaccineTypeSer(YmVaccineTypeWithBLOBs ymVaccineTypeWithBLOBs,Page<Object> page) {
		   return ymVaccineTypeMapper.queryVaccineTypeMapper(ymVaccineTypeWithBLOBs);
	}*/
	@Override
	public Page<Object> queryVaccineTypeSer(YmVaccineTypeWithBLOBs ymVaccineTypeWithBLOBs,Page<Object> page) {
		Map<String,Object> map = new HashMap<String, Object>();  
	  /*map.put("YmVaccineTypeWithBLOBs",ymVaccineTypeWithBLOBs);
		map.put("page",page);*/
		page.setResults(ymVaccineTypeMapper.queryVaccineTypeMapper(ymVaccineTypeWithBLOBs,page));
		return page;
	}

	/**
	 * 疫苗周岁的详情查询
	 * 
	 * @param YmVaccine ymVaccine
	 *           
	 * @return List<YmVaccine>
	 */
	/*@Override
	public List<YmVaccine> queryVaccineSer(YmVaccine ymVaccine) {
		  return ymVaccineMapper.queryVaccineMapper(ymVaccine);
	}*/
	@Override
	public Page<Object> queryVaccineSer(YmVaccine ymVaccine,Page<Object> page) {
		page.setResults(ymVaccineMapper.queryVaccineMapper(ymVaccine,page));
		return page;
	}

	/**
	 * 疫苗关联查询(包括详情)
	 * 
	 * @param YmVaccine YmVaccine
	 *           
	 * @return List<YmVaccine>
	 */
	@Override
	public List<YmVaccine> queryVaccineRelevanceDetailSer(String[] vaccines) {
		 Map<String,Object> map = new HashMap<String, Object>();
		 map.put("vaccineIds",vaccines);
		 return ymVaccineMapper.queryVaccineRelevanceDetailMapper(map);
	}
	/**
	 * 接种证查询(移动端)
	 * 
	 * @param age
	 *           
	 * @return List<YmVaccine>
	 */
	@Override
	public List<YmVaccine> queryVaccineInoculationPlanSer(String age,YmUserChild ymUserChild) {
		 Map<String,Object> map = new HashMap<String, Object>();
		 String[] TIMES = Const.TIMESECTION;
		 //查出一个娃娃的出生日期(测试)
	     String birthday = ymUserChild.getChildBirthday();
	     List<YmVaccine> ymVaccines = new ArrayList<YmVaccine>();
	     try {
		 String ageStage = Util.getAgeStage(Util.getAge(birthday));
		 Integer index = -1;
		 for(String timeStage : TIMES){
		 if(timeStage.equals(ageStage)){
		 index ++;	
		 break;
		 }else{
		 index ++;
		  }
		 }
		 List<String> TIMES_list1= Arrays.asList(TIMES);
		 List<String> newList = TIMES_list1.subList(index,TIMES_list1.size());
		 map.put("list",newList);
		 map.put("age",age);
		 ymVaccines = ymVaccineMapper.queryVaccineInoculationPlanMapper(map);
		 for(int i = 0;i < ymVaccines.size();i++){
		 String a = ymVaccines.get(i).getVaccineTypeName();
		 String[] pt = a.split("/");
			/* 出生","1月龄","2月龄","3月龄","4月龄",
			    "5月龄","6月龄","8月龄","9月龄","11月龄","1周岁","1岁半","2周岁","3周岁","4周岁","6周岁","9周岁"*/
		 if(pt[0].equals("出生")){
			 ymVaccines.get(i).setSqu(0);
		 }else if(pt[0].equals("1月龄")){
			 ymVaccines.get(i).setSqu(1);
		 }else if(pt[0].equals("2月龄")){
			 ymVaccines.get(i).setSqu(2);
		 }else if(pt[0].equals("3月龄")){
			 ymVaccines.get(i).setSqu(3);
		 }else if(pt[0].equals("4月龄")){
			 ymVaccines.get(i).setSqu(4);
		 }else if(pt[0].equals("5月龄")){
			 ymVaccines.get(i).setSqu(5);
		 }else if(pt[0].equals("6月龄")){
			 ymVaccines.get(i).setSqu(6);
		 }else if(pt[0].equals("8月龄")){
			 ymVaccines.get(i).setSqu(7);
		 }else if(pt[0].equals("9月龄")){
			 ymVaccines.get(i).setSqu(8);
		 }else if(pt[0].equals("11月龄")){
			 ymVaccines.get(i).setSqu(9);
		 }else if(pt[0].equals("1周岁")){
			 ymVaccines.get(i).setSqu(10);
		 }else if(pt[0].equals("1岁半")){
			 ymVaccines.get(i).setSqu(11);
		 }else if(pt[0].equals("2周岁")){
			 ymVaccines.get(i).setSqu(12);
		 }else if(pt[0].equals("3周岁")){
			 ymVaccines.get(i).setSqu(13);
		 }else if(pt[0].equals("4周岁")){
			 ymVaccines.get(i).setSqu(14);
		 }else if(pt[0].equals("6周岁")){
			 ymVaccines.get(i).setSqu(15);
		 }else if(pt[0].equals("9周岁")){
			 ymVaccines.get(i).setSqu(16);
		 }
		
		 }
		//排序(倒序)
		ymVaccines.sort(new Comparator<YmVaccine>() {//Comparator 比较器. 需要实现比较方法
        @Override
        public int compare(YmVaccine o1, YmVaccine o2) {
            return o1.getSqu()-o2.getSqu();//从大到小
        }
        });
		 }catch (Exception e) {
		 throw new IllegalArgumentException(String.format(ErrorUtil.VACCINE_ERROR));
		 }
		 return ymVaccines;
	}
	
	/**
	 * 接种历史查询(移动端)
	 * 
	 * @param vaccineId,parentId
	 *           
	 * @return List<YmVaccine>
	 */
	//全局变量前
	List<YmVaccine> YmVaccinesBefore = new ArrayList<YmVaccine>();
	//全局变量前
	List<YmVaccine> YmVaccinesAfter = new ArrayList<YmVaccine>();
	@Override
	public List<YmVaccine> queryVaccineHistoryPlanSer(Long vaccineTypeId,Long parentId,Long childrenId) {
		//初始化全局
		/*YmVaccinesBefore = new ArrayList<YmVaccine>();
		YmVaccinesAfter = new ArrayList<YmVaccine>();
		List<YmVaccine> YmVaccineTree = new ArrayList<YmVaccine>();
		List<YmVaccine> YmVaccineTreeAfter = new ArrayList<YmVaccine>();
		YmVaccineTree = this.ymVaccineMapper.queryVaccineHistoryPlanMapper(vaccineId);
		YmVaccinesBefore.addAll(YmVaccineTree);
		YmVaccineTreeAfter = this.ymVaccineMapper.queryVaccineHistoryPlanAfterMapper(parentId);
		YmVaccinesAfter.addAll(YmVaccineTreeAfter);
		beforePackagingSelectTree(vaccineId,YmVaccineTree);
		afterPackagingSelectTree(parentId,YmVaccineTreeAfter);
		YmVaccinesBefore.addAll(YmVaccinesAfter);
		return YmVaccinesBefore;*/
	//	String childId = "123";
	//	YmUser ymUser = (YmUser) ThreadLocalUtil.getUser();
	//	Long userId = ymUser.getUserId();
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("childId",childrenId);
		map.put("vaccineTypeId",vaccineTypeId);
		List<YmVaccine> YmVaccineTree = ymVaccineMapper.queryVaccineHistoryPlanAllaMapper(map);
		List<Long> vaccineIds = new ArrayList<Long>();
		for(int i = 0;i < YmVaccineTree.size();i++){
			vaccineIds.add(YmVaccineTree.get(i).getVaccineId());
		}
		if(vaccineIds.size()==0){
		vaccineIds.add(0l);
		}
		map.put("list",vaccineIds);
		List<YmVaccine> YmVaccineb = ymVaccineMapper.queryVaccineHistoryPlanAllbMapper(map);
		for(int j = 0;j < YmVaccineb.size();j++){
			YmVaccineb.get(j).setVaccinationTime("");
		}
		YmVaccineTree.addAll(YmVaccineb);
		return YmVaccineTree;
		
	}
	
	/**
	 * 递归查询(选择前)
	 *
	 * @param YmVaccineTree
	 */
	
	private void beforePackagingSelectTree(Long vaccineId,List<YmVaccine> treeDtos) {
		if (treeDtos.size() > 0) {
			YmVaccinesBefore.addAll(treeDtos);
			for (YmVaccine treeDto : treeDtos) {
				List<YmVaccine> trees = this.ymVaccineMapper.queryVaccineHistoryPlanMapper(treeDto.getVaccineId());
				beforePackagingSelectTree(treeDto.getVaccineId(),trees);
			}
		}
	}
	
	/**
	 * 递归查询(选择后)
	 *
	 * @param YmVaccineTree
	 */
	private void afterPackagingSelectTree(Long parentId,List<YmVaccine> treeDtos) {
		if (treeDtos.size() > 0) {
			YmVaccinesAfter.addAll(treeDtos);
			for (YmVaccine treeDto : treeDtos) {
				List<YmVaccine> trees = this.ymVaccineMapper.queryVaccineHistoryPlanMapper(treeDto.getParentId());
				beforePackagingSelectTree(treeDto.getParentId(),trees);
			}
		}
	}

	/**
	 * 查询历史疫苗(移动端)
	 * 
	 * @param 
	 *           
	 * @return List<YmVaccine>
	 */
	@Override
	public List<YmVaccine> queryVaccineHistoryPlanAllSer(Long childrenId) {
		List<YmVaccine> ymVaccines = new ArrayList<YmVaccine>();
		//默认孩子的ID(测试)
		ymVaccines = ymVaccineMapper.queryVaccineHstoryPlanAllMapper(childrenId);
		return ymVaccines;
	}

	/**
	 * 接种参考查询(移动端)
	 * 
	 * @param 
	 *           
	 * @return List<YmVaccine>
	 */
	@Override
	public List<YmVaccine> queryInoculationConsultSer(){
		String[] TIMES = Const.TIMESECTION;
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("list",TIMES);
		map.put("age",null);
		List<YmVaccine> ymVaccines = ymVaccineMapper.queryVaccineInoculationPlanMapper(map);
		for(int i = 0;i < ymVaccines.size();i++){
			 String a = ymVaccines.get(i).getVaccineTypeName();
			 String[] pt = a.split("/");
			 if(pt[0].equals("出生")){
				 ymVaccines.get(i).setSqu(0);
			 }else if(pt[0].equals("1月龄")){
				 ymVaccines.get(i).setSqu(1);
			 }else if(pt[0].equals("2月龄")){
				 ymVaccines.get(i).setSqu(2);
			 }else if(pt[0].equals("3月龄")){
				 ymVaccines.get(i).setSqu(3);
			 }else if(pt[0].equals("4月龄")){
				 ymVaccines.get(i).setSqu(4);
			 }else if(pt[0].equals("5月龄")){
				 ymVaccines.get(i).setSqu(5);
			 }else if(pt[0].equals("6月龄")){
				 ymVaccines.get(i).setSqu(6);
			 }else if(pt[0].equals("8月龄")){
				 ymVaccines.get(i).setSqu(7);
			 }else if(pt[0].equals("9月龄")){
				 ymVaccines.get(i).setSqu(8);
			 }else if(pt[0].equals("11月龄")){
				 ymVaccines.get(i).setSqu(9);
			 }else if(pt[0].equals("1周岁")){
				 ymVaccines.get(i).setSqu(10);
			 }else if(pt[0].equals("1岁半")){
				 ymVaccines.get(i).setSqu(11);
			 }else if(pt[0].equals("2周岁")){
				 ymVaccines.get(i).setSqu(12);
			 }else if(pt[0].equals("3周岁")){
				 ymVaccines.get(i).setSqu(13);
			 }else if(pt[0].equals("4周岁")){
				 ymVaccines.get(i).setSqu(14);
			 }else if(pt[0].equals("6周岁")){
				 ymVaccines.get(i).setSqu(15);
			 }else if(pt[0].equals("9周岁")){
				 ymVaccines.get(i).setSqu(16);
			  }
			 }
			//排序(倒序)
			ymVaccines.sort(new Comparator<YmVaccine>() {//Comparator 比较器. 需要实现比较方法
	        @Override
	        public int compare(YmVaccine o1, YmVaccine o2) {
	            return o1.getSqu()-o2.getSqu();//从大到小
	        }
	        });
		return ymVaccines;
	}

	/**
	 * 新增历史疫苗(管理端)
	 * 
	 * @param YmChildrenOrderVaccination YmChildrenOrderVaccination
	 *           
	 * @return List<YmVaccine>
	 */
	@Override
	public void insertYmChildrenOrderVaccinationSer(YmChildrenOrderVaccination YmChildrenOrderVaccination) {
		ymChildrenOrderVaccinationMapper.insertSelective(YmChildrenOrderVaccination);
	}

	/**
	 * 疫苗关联查询(包括详情)*找到某个疫苗所有所有关联疫苗(管理端)
	 * 
	 * @param YmVaccine YmVaccine
	 *           
	 * @return List<YmVaccine>
	 */
	@Override
	public List<YmVaccineTypeWithBLOBs> queryVaccineRelevanceDetailPcSer(YmVaccine ymVaccine){
		return ymVaccineTypeMapper.queryVaccineRelevanceDetailPcMapper(ymVaccine);
	}

	/**
	 * 根据年龄查询参考计划
	 * 
	 * @param String age
	 *           
	 * @return List<YmVaccine>
	 */
	@Override
	public List<YmVaccine> queryVaccinePlanAgeSer(String age){

		return ymVaccineMapper.queryVaccinePlanAgeMapper(age);
	}

	/**
	 * 用户手动录入接种记录(根据疫苗ID，孩子Id查询所有未接种的剂次)
	 *
	 * @param vaccineTypeId, childId
	 *
	 * @return
	 */
	@Override
	public List<YmVaccine> queryAgentByVaccineTypeId(Long vaccineTypeId, Long childId) {
		Assert.notNull(vaccineTypeId,String.format(ErrorUtil.PARAMETER_NULL,"vaccineTypeId"));
		Assert.notNull(childId,String.format(ErrorUtil.PARAMETER_NULL,"childId"));
		return ymVaccineMapper.queryAgentByVaccineTypeId(vaccineTypeId, childId);
	}


	/**
     * 用户手动录入接种记录
     *
     * @param  ymChildrenOrderVaccination
     *
     * @return
     */
    @Override
    public Integer insterYmChildrenOrderVaccinationByUserInfo(YmChildrenOrderVaccination ymChildrenOrderVaccination) {
        Long  childId = ymChildrenOrderVaccination.getChildId();
        Long vaccineId = ymChildrenOrderVaccination.getVaccineId();
        Assert.notNull(childId,String.format(ErrorUtil.PARAMETER_NULL,"childId"));
        Assert.notNull(vaccineId,String.format(ErrorUtil.PARAMETER_NULL,"vaccineId"));
        Short state = 1;
		ymChildrenOrderVaccination.setState(state);
        return ymChildrenOrderVaccinationMapper.insertSelective(ymChildrenOrderVaccination);
    }

	/**
	 * 用户手动修改接种记录
	 *
	 * @param  ymChildrenOrderVaccination
	 *
	 * @return
	 */
	@Override
	public Integer updateYmChildrenOrderVaccinationById(YmChildrenOrderVaccination ymChildrenOrderVaccination) {
		Long orderVaccinationId = ymChildrenOrderVaccination.getOrderVaccinationId();
		Assert.notNull(orderVaccinationId,String.format(ErrorUtil.PARAMETER_NULL,"orderVaccinationId"));
		return ymChildrenOrderVaccinationMapper.updateByPrimaryKeySelective(ymChildrenOrderVaccination);
	}
}
